<?php

namespace App\Http\Controllers\Admin;

use App\Http\Filters\BoardFilter;
use App\Models\Board;
use App\Http\Requests\BoardRequest;
use App\Http\Resources\BoardResource;
use App\Models\Event;
use App\Models\Housings;
use App\Models\HousingsOrder;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BoardController extends Controller
{
    public function index(BoardFilter $filter,BoardRequest $request)
    {
        $data['housingsCount']=Housings::count();
        $data['housingsType2']=HousingsOrder::where('order_type','=',1)->count();
        $data['housingsIsUp2']=Housings::where('housings_is_up','=',2)->count();
        $today = Carbon::today();
        $data['orderToday']=Order::whereDate('created_at', $today)->count();
        $data['orderTodayT']=Order::whereDate('created_at', $today)->where('order_type','=',3)->count();
        $data['eventCount']=Event::count();


        $data['orderCount'] = $totalOrders = Order::count();
        $orderTypesCount = Order::select('order_source_type', DB::raw('count(*) as count'))
            ->groupBy('order_source_type')
            ->get();
        $orderTypePercentages = $orderTypesCount->map(function ($orderType) use ($totalOrders) {
            $percentage = ($orderType->count / $totalOrders) * 100;
            return [
                'orderType' => $orderType->order_type,
                'percenTage' => round($percentage, 2) // 保留两位小数
            ];
        });
        return $this->ok($data);
    }

    public function boardmoney(BoardRequest $request)
    {
        $now = Carbon::now();
        $filter=$request->validated();
        switch ($filter['date']){
            case 1:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();
                break;
            case 2:
                $startOf = $now->copy()->startOfMonth();
                $endOf = $now->copy()->endOfMonth();
                break;
            case 3:
                $startOf = $now->copy()->startOfYear();
                $endOf = $now->copy()->endOfYear();
                break;

            default:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();

        }
        $data['Sales'] = Order::whereBetween('created_at', [$startOf, $endOf])
            ->select(DB::raw('DATE(created_at) as orderDate'), DB::raw('SUM(money) as dailySales'))
            ->groupBy('orderDate')
            ->orderBy('orderDate')
            ->get()->toarray();
        return $this->ok($data);
    }

    public function boardusertype(BoardRequest $request)
    {
        $now = Carbon::now();
        $filter=$request->validated();
        switch ($filter['date']){
            case 1:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();
                break;
            case 2:
                $startOf = $now->copy()->startOfMonth();
                $endOf = $now->copy()->endOfMonth();
                break;
            case 3:
                $startOf = $now->copy()->startOfYear();
                $endOf = $now->copy()->endOfYear();
                break;

            default:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();

        }
        $totalOrders = Order::whereBetween('created_at', [$startOf, $endOf])->count();

        $orderTypesCount = Order::whereBetween('created_at', [$startOf, $endOf])->select('order_source_type', DB::raw('count(*) as count'))
            ->groupBy('order_source_type')
            ->get();

        $orderTypePercentages = $orderTypesCount->map(function ($orderSourceType) use ($totalOrders) {
            $percentage = ($orderSourceType->count / $totalOrders) * 100;
            return [
                'value'=>$orderSourceType->count,
                'name' => $orderSourceType->order_source_type,
                'rate' => round($percentage, 2).'%' // 保留两位小数
            ];
        });

        return $this->ok(['data'=>$orderTypePercentages,'count'=>$totalOrders]);
    }

    public function boardorder(BoardRequest $request)
    {
        $now = Carbon::now();
        $filter = $request->validated();
        switch ($filter['date']) {
            case 1:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();
                break;
            case 2:
                $startOf = $now->copy()->startOfMonth();
                $endOf = $now->copy()->endOfMonth();
                break;
            case 3:
                $startOf = $now->copy()->startOfYear();
                $endOf = $now->copy()->endOfYear();
                break;

            default:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();

        }
        $data['Salesone'] = Order::whereBetween('created_at', [$startOf, $endOf])
            ->select(DB::raw('DATE(created_at) as orderDate'), DB::raw('count(1)+1 as tadayCount'))
            ->groupBy('orderDate')
            ->orderBy('orderDate')
            ->get()->toarray();

        $data['Salestwo'] = Order::whereBetween('created_at', [$startOf, $endOf])
            ->select(DB::raw('DATE(created_at) as orderDate'), DB::raw('count(1) as tadayCount'))
            ->groupBy('orderDate')
            ->orderBy('orderDate')
            ->get()->toarray();
        return $this->ok($data);
    }

        public function boardcall(BoardRequest $request)
    {
        $now = Carbon::now();
        $filter=$request->validated();
        switch ($filter['date']){
            case 1:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();
                break;
            case 2:
                $startOf = $now->copy()->startOfMonth();
                $endOf = $now->copy()->endOfMonth();
                break;
            case 3:
                $startOf = $now->copy()->startOfYear();
                $endOf = $now->copy()->endOfYear();
                break;

            default:
                $startOf = $now->copy()->startOfWeek();
                $endOf = $now->copy()->endOfWeek();
        }
        $data['Sales'] = Order::whereBetween('created_at', [$startOf, $endOf])
            ->select(DB::raw('DATE(created_at) as orderDate'), DB::raw('SUM(money) as dailySales'))
            ->groupBy('orderDate')
            ->orderBy('orderDate')
            ->get()->toarray();
        return $this->ok($data);
    }


}
